<%-- 
    Document   : createComment
    Created on : Dec 9, 2011, 1:54:51 AM
    Author     : Owner
--%>

<%@page import="java.sql.*"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<% if(session.getAttribute("LoggedIn")==null)
       {
    response.sendRedirect("index.html");
    return;
        }

%>
<%
    String postID = request.getParameter("PST");
    int usrID = Integer.parseInt(session.getAttribute("UserID").toString());
    String comment = request.getParameter("commentText");
    //String post = request.getParameter("postText");
    //session.setAttribute("CIRC",circleID);
    Connection conn = null;
    try
    {
        conn = new DBClasses.GetConnection().getConnection();
        //get pageID
        String sql ="SELECT PageID FROM Posts WHERE PostID = "+postID;
        Statement stmt = conn.createStatement();
        ResultSet rs = stmt.executeQuery(sql);
        rs.next();
        int pageID = rs.getInt("PageID");
        
        sql= "SELECT AssociatedCircleID FROM Pages WHERE PageID = "+pageID;

        rs = conn.createStatement().executeQuery(sql);
        rs.next();
        String circleID = rs.getString("AssociatedCircleID");
        session.setAttribute("CIRC", circleID); //to go back to circlepage
        
        sql = "INSERT INTO Comments (Content, Author, PostID) "
                +"VALUES('"+comment+"', "+usrID+","+postID+")";
        
        conn.createStatement().execute(sql);
       
    }
    catch(Exception e)
    {
         
        e.printStackTrace();
        response.sendRedirect("CirclePage.jsp");
    }
    
    finally
    {
        try 
        { 
            conn.commit();
            conn.close();
            
            response.sendRedirect("CirclePage.jsp"); 
        }
        catch(Exception e) 
        {  
            e.printStackTrace();
            response.sendRedirect("CirclePage.jsp"); 

        }
    }
%>
